<?php
class App_Page_Form_AdminBlockEdit extends Main_Form
{
	/**
	 * @var App_Page_Service_Block
	 */
	protected $_serviceBlock;
	
	/**
	 * @var App_Page_Service_Page
	 */
	protected $_servicePage;
	
	/**
     * @var array
     */
    protected $_decoratorsForm  = array(
        'PrepareElements',
        array('ViewScript', array('viewScript' => '_form/admin-block-edit.phtml')),
    );
	
	/**
	 * @var App_Admin_Service_Lang
	 */
	protected $_serviceLang;
	
	/**
	 * @var App_Admin_Service_Plugin
	 */
	protected $_servicePlugin;
	
	/**
	 * @return void
	 */
	public function init()
	{
		parent::init();
		
		$this->_serviceBlock = new App_Page_Service_Block();
		$this->_serviceLang = new App_Admin_Service_Lang();
		$this->_servicePlugin = new App_Admin_Service_Plugin();
		$this->_servicePage = new App_Page_Service_Page();
		
		// --- pages --- {{
		$pageList = $this->_servicePage->getAll();
		$pageArr = array();
		foreach ($pageList as $pageItem) {
			$pageArr[$pageItem['id']] = str_repeat(" . ", ($pageItem['level'] - 1)*6) 
									  . '/' 
				                      . $pageItem['name'];
		}
		// --- pages --- }}
		
		$this->setMethod('post');
		$this->setDecorators($this->_decoratorsForm);
		
		$this->addElement('hidden', 'old_name', array(
			'order'	=> 5,
			'decorators' => $this->_decoratorsHidden
		));
		
		$this->addElement('text', 'name', array(
			'label'	=> 'Имя',
			'required' => true,
			'validators' => array(
				array('validator' => 'StringLength', 'options' => array(0, 255)),
				array(
					'validator' => 'UniqueItem', 'options' => array (
                    	'App_Page_Service_Block', 'old_name', array('page_id' => 'page_id'),
                        'Блок с таким названием на для текущей странице уже существует' )
                )
			),
			'order'	=> 10,
		));
		
		$this->addElement('select', 'page_id', array(
			'label'	=> 'Страница',
			'required' => true,
			'multioptions' => $pageArr, 
			'order'	=> 20,
		));
		
		$this->addElement('select', 'lang_id', array(
			'label'	=> 'Язык',
			'multioptions' => array(0 => '- Все -') + $this->_serviceLang->getAllIdAndName(), 
			'order'	=> 25,
		));
		
		$this->addElement('select', 'plugin_id', array(
			'label'	=> 'Плагин',
			'multioptions' => array(0 => '- Нет -') + $this->_servicePlugin->getAllIdAndName(), 
			'order'	=> 30,
		));
		
		$this->addElement('text', 'description', array(
			'label' => 'Описание',
			'order'	=> 40,			
		));
		
		$this->addElement('textarea', 'item_content', array(
			'label'	=> 'Содержимое',
			'rows' => 10,
            'cols' => 70,
			'maxlength' => 255,
			'required' => false,
			'escape' => false,
			'order'	=> 50,
		));
		
		$this->addElement('checkbox', 'f_active', array(
			'label'	=> 'Статус',
			'order'	=> 60,
		));
		
		$this->addElement('submit', 'addButton', array(
			'label' => 'Сохранить',
			'order'	=> 100,
		));
	}
	
	/**
	 * @param array $data
     * @return Main_Form
     */
    public function setFormValues($data)
    {
		$data['old_name'] = $data['name'];
    	return parent::setFormValues($data);
    }

}